<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Jenkins整合sonarQube - macdfree&#39;s blog</title>
  <meta name="renderer" content="webkit" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>

<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">


<meta name="author" content="macd" /><meta name="description" content="Jenkins整合SonarQube" />

  <meta name="keywords" content="blog, macd, even" />


<meta name="baidu-site-verification" content="Ak74Sk61bk" />
<meta name="google-site-verification" content="fAeFHpkrhfFS8LxdzXy0EtAPcVStv0faGn238sRF3L8" />


<meta name="generator" content="Hugo 0.54.0" />


<link rel="canonical" href="/posts/jenkins%E6%95%B4%E5%90%88sonarqube/" />

<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">




<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>


<link href="/dist/even.min.css?v=3.1.1" rel="stylesheet">
<link href="/lib/fancybox/jquery.fancybox-3.1.20.min.css" rel="stylesheet">




<meta property="og:title" content="Jenkins整合sonarQube" />
<meta property="og:description" content="Jenkins整合SonarQube" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/posts/jenkins%E6%95%B4%E5%90%88sonarqube/" />
<meta property="article:published_time" content="2018-07-28T17:14:20&#43;08:00"/>
<meta property="article:modified_time" content="2018-07-28T17:14:20&#43;08:00"/>

<meta itemprop="name" content="Jenkins整合sonarQube">
<meta itemprop="description" content="Jenkins整合SonarQube">


<meta itemprop="datePublished" content="2018-07-28T17:14:20&#43;08:00" />
<meta itemprop="dateModified" content="2018-07-28T17:14:20&#43;08:00" />
<meta itemprop="wordCount" content="1102">



<meta itemprop="keywords" content="sonarqube,jenkins," />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Jenkins整合sonarQube"/>
<meta name="twitter:description" content="Jenkins整合SonarQube"/>

<!--[if lte IE 9]>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
<![endif]-->

<!--[if lt IE 9]>
  <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->

</head>
<body>
  <div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/" class="logo">macdfree&#39;s blog</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>
<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    <a href="/">
        <li class="mobile-menu-item">首页</li>
      </a><a href="/categories/">
        <li class="mobile-menu-item">分类</li>
      </a><a href="/tags/">
        <li class="mobile-menu-item">标签</li>
      </a><a href="/archive/">
        <li class="mobile-menu-item">归档</li>
      </a>
  </ul>
</nav>
  <div class="container" id="mobile-panel">
    <header id="header" class="header">
        <div class="logo-wrapper">
  <a href="/" class="logo">macdfree&#39;s blog</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
        <a class="menu-item-link" href="/">首页</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/categories/">分类</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/tags/">标签</a>
      </li><li class="menu-item">
        <a class="menu-item-link" href="/archive/">归档</a>
      </li>
  </ul>
</nav>
    </header>

    <main id="main" class="main">
      <div class="content-wrapper">
        <div id="content" class="content">
          <article class="post">
    
    <header class="post-header">
      <h1 class="post-title">Jenkins整合sonarQube</h1>

      <div class="post-meta">
        <span class="post-time"> 2018-07-28 </span>
        <div class="post-category">
          
            <a href="/categories/%E6%8C%81%E7%BB%AD%E9%9B%86%E6%88%90/"> 持续集成 </a>
          
        </div>
        <span class="more-meta"> 约 1102 字 </span>
        <span class="more-meta"> 预计阅读 3 分钟 </span>
        <span id="busuanzi_container_page_pv" class="more-meta"> <span id="busuanzi_value_page_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次阅读 </span>
      </div>
    </header>

    
    
<div class="post-toc" id="post-toc">
  <h2 class="post-toc-title">文章目录</h2>
  
  <div class="post-toc-content always-active">
    <nav id="TableOfContents">
<ul>
<li><a href="#安装sonarqube">安装SonarQube</a>
<ul>
<li><a href="#系统要求">系统要求</a></li>
<li><a href="#下载安装包">下载安装包</a></li>
<li><a href="#centos7环境下安装">CentOS7环境下安装</a></li>
</ul></li>
<li><a href="#sonarqube配置">SonarQube配置</a>
<ul>
<li><a href="#更新插件">更新插件</a></li>
<li><a href="#配置svn账号密码">配置SVN账号密码</a></li>
<li><a href="#生成token">生成Token</a></li>
</ul></li>
<li><a href="#jenkins整合sonarqube">Jenkins整合SonarQube</a>
<ul>
<li><a href="#sonarqube插件安装配置">SonarQube插件安装配置</a></li>
<li><a href="#项目配置">项目配置</a></li>
</ul></li>
</ul>
</nav>
  </div>
</div>

    
    <div class="post-content">
      <p>本文主要记录在CentOS7中安装SonarQube，以及将SonarQube整合到Jenkins的过程</p>

<h1 id="安装sonarqube">安装SonarQube</h1>

<h2 id="系统要求">系统要求</h2>

<p>详细要求参见：<a href="https://docs.sonarqube.org/display/SONAR/Requirements">https://docs.sonarqube.org/display/SONAR/Requirements</a></p>

<p>这边简要描述一下基本的要求：</p>

<ol>
<li>对于小型团队至少需要4GB内存，对于企业用户则需要8核16GB配置；</li>
<li>通常使用的Oracle JRE，版本必须是JRE8</li>
<li>由于SonarQube使用到了Elasticsearch，对于Linux环境需要设置：</li>
</ol>

<pre><code class="language-bash">sysctl -w vm.max_map_count=262144
sysctl -w fs.file-max=65536
ulimit -n 65536
ulimit -u 2048
</code></pre>

<h2 id="下载安装包">下载安装包</h2>

<p>下载路径：<a href="https://www.sonarqube.org/downloads/">https://www.sonarqube.org/downloads/</a></p>

<p>笔者下载的是长期支持版本6.7.4</p>

<h2 id="centos7环境下安装">CentOS7环境下安装</h2>

<p>由于Elasticsearch不能使用root用户运行，所以先需要新建用户，可参考<a href="/wiki/linux/%E7%94%A8%E6%88%B7%E6%96%B0%E5%A2%9E%E5%B9%B6%E6%8E%88%E6%9D%83/">用户新增并授权</a>。使用此用户下载安装包并解压缩，解压缩后目录结构如下图所示</p>

<p><img src="/images/sonarqube目录结构.png" alt="sonarqube目录结构" /></p>

<p>接下来需要创建数据库，笔者使用MySQL数据库，创建完成后需要修改配置文件</p>

<pre><code class="language-bash">vim conf/sonar.properties
</code></pre>

<p><img src="/images/sonarube数据库修改.png" alt="sonarube数据库修改" /></p>

<p>在启动之前，需要赋予一下执行权限</p>

<pre><code class="language-bash">chmod +x bin/linux-x86-64/sonar.sh
chmod +x bin/linux-x86-64/wrapper
chmod +x elasticsearch/bin/*
</code></pre>

<p>以上准备工作完成后就可以启动SonarQube了</p>

<pre><code class="language-bash">bin/linux-x86-64/sonar.sh start
</code></pre>

<p>稍等片刻启动完成后，即可访问<code>http://ipaddress:9000</code></p>

<h1 id="sonarqube配置">SonarQube配置</h1>

<h2 id="更新插件">更新插件</h2>

<p>使用admin/admin登录系统，点击配置/应用市场进行插件的更新和安装。建议可以安装中文语言包以便于使用，在搜索框中输入<code>chinese</code>，选择<code>Chinese Pack</code>进行安装。另外，对于需要用到插件也可以进行更新。注意，安装和更新插件需要重启后才生效。</p>

<p><img src="/images/sonarqube更新插件.png" alt="sonarqube更新插件" /></p>

<h2 id="配置svn账号密码">配置SVN账号密码</h2>

<p>由于代码使用SVN进行管理，在分析代码时SonarQube需要有SVN权限，设置如下图所示，首先需要启用SCM，然后配置用户名和密码。</p>

<p><img src="/images/sonarqub设置svn账号.png" alt="sonarqube设置svn账号" /></p>

<h2 id="生成token">生成Token</h2>

<p>由于下面需要和Jenkins进行整合，所以此处需要生成一个Token供Jenkins使用。生成后切记将此Token保存下来，供Jenkins整合时使用。</p>

<p><img src="/images/sonarqubeToken设置.png" alt="sonarqubeToken设置" /></p>

<p>至此，SonarQube的配置基本完成，下面就轮到Jenkins上场了。</p>

<h1 id="jenkins整合sonarqube">Jenkins整合SonarQube</h1>

<h2 id="sonarqube插件安装配置">SonarQube插件安装配置</h2>

<p>在Jenkins中点击系统管理/管理插件，搜索<code>SonarQube Scanner</code>安装，安装完成重启后，点击系统管理/系统设置，定位到如下图所示位置</p>

<p><img src="/images/sonarqube整合Jenkins配置.png" alt="sonarqube整合Jenkins配置" /></p>

<p>其中<code>Server URL</code>即SonarQube的访问地址，<code>Server authentication token</code>即上一节中生成的Token。</p>

<h2 id="项目配置">项目配置</h2>

<p>选择需要分析的项目，点击配置，添加构建后步骤，如下图所示</p>

<p><img src="/images/sonarqube整合Jenkins项目配置.png" alt="sonarqube整合Jenkins项目配置" /></p>

<p><img src="/images/sonarqube整合Jenkins项目配置2.png" alt="sonarqube整合Jenkins项目配置2" /></p>

<pre><code class="language-ini">sonar.projectKey=projectKey
sonar.projectName=projectName
sonar.projectVersion=1.0
sonar.language=java
sonar.java.binaries=$WORKSPACE/target/classes/
sonar.sources=$WORKSPACE/src
</code></pre>

<p>配置完成后点击立即构建，构建成功后可在SonarQube中看到代码的分析结果。</p>

<p><img src="/images/sonarube分析结果.png" alt="sonarube分析结果" /></p>
    </div>

    
    <div class="post-copyright">
  <p class="copyright-item">
    <span class="item-title">文章作者</span>
    <span class="item-content">macd</span>
  </p>
  <p class="copyright-item">
    <span class="item-title">上次更新</span>
    <span class="item-content">2018-07-28</span>
  </p>
  
  
</div>

    
    
<div class="post-reward">
  <input type="checkbox" name="reward" id="reward" hidden />
  <label class="reward-button" for="reward">赞赏支持</label>
  <div class="qr-code">
    
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/wechat-qr-code.png">
        <span>微信打赏</span>
      </label>
    
      <label class="qr-code-image" for="reward">
        <img class="image" src="/img/reward/alipay-qr-code.png">
        <span>支付宝打赏</span>
      </label>
  </div>
</div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
          <a href="/tags/sonarqube/">sonarqube</a>
          
          <a href="/tags/jenkins/">jenkins</a>
          
        </div>

      
      <nav class="post-nav">
        
          <a class="prev" href="/posts/maven%E6%89%93%E5%8C%85%E6%B7%BB%E5%8A%A0svn%E7%89%88%E6%9C%AC%E5%8F%B7%E5%92%8C%E6%9E%84%E5%BB%BA%E6%97%B6%E9%97%B4/">
            <i class="iconfont icon-left"></i>
            <span class="prev-text nav-default">Maven打包添加SVN版本号和构建时间</span>
            <span class="prev-text nav-mobile">上一篇</span>
          </a>
        
          <a class="next" href="/posts/vps%E5%88%9D%E5%A7%8B%E5%8C%96%E9%85%8D%E7%BD%AE%E8%AE%B0%E5%BD%95/">
            <span class="next-text nav-default">VPS初始化配置记录</span>
            <span class="prev-text nav-mobile">下一篇</span>
            <i class="iconfont icon-right"></i>
          </a>
      </nav>
    </footer>
  </article>
        </div>
        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="social-links">
      <a href="mailto:macdfree@163.com" class="iconfont icon-email" title="email"></a>
      <a href="https://github.com/MACDfree" class="iconfont icon-github" title="github"></a>
  <a href="/index.xml" type="application/rss+xml" class="iconfont icon-rss" title="rss"></a>
</div>

<div class="copyright">
  <span class="power-by">
    由 <a class="hexo-link" href="https://gohugo.io">Hugo</a> 强力驱动
  </span>
  <span class="division">|</span>
  <span class="theme-info">
    主题 - 
    <a class="theme-link" href="https://github.com/olOwOlo/hugo-theme-even">Even</a>
  </span>

  <div class="busuanzi-footer">
    <span id="busuanzi_container_site_pv"> 本站总访问量 <span id="busuanzi_value_site_pv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 次 </span>
    <span class="division">|</span>
    <span id="busuanzi_container_site_uv"> 本站总访客数 <span id="busuanzi_value_site_uv"><img src="/img/spinner.svg" alt="spinner.svg"/></span> 人 </span>
  </div>

  <span class="copyright-year">
    &copy; 
    
      2014 - 
    2019
    <span class="heart">
      <i class="iconfont icon-heart"></i>
    </span>
    <span class="author">macd</span>
    <span class="division">|</span>
    <span style="margin-left: 5px;">
      <img alt="buildstatus" src="https://travis-ci.org/MACDfree/hugoblog.svg?branch=master">
    </span>
  </span>
</div>
    </footer>

    <div class="back-to-top" id="back-to-top">
      <i class="iconfont icon-up"></i>
    </div>
  </div>
  
<script src="/lib/highlight/highlight.pack.js?v=20171001"></script><script type="text/javascript" src="/lib/jquery/jquery-3.2.1.min.js"></script>
  <script type="text/javascript" src="/lib/slideout/slideout-1.0.1.min.js"></script>
  <script type="text/javascript" src="/lib/fancybox/jquery.fancybox-3.1.20.min.js"></script>
<script type="text/javascript" src="/dist/even.min.js?v=3.1.1"></script>








</body>
</html>
